<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NewsController
 *
 * @author LeVinh
 */
include_once APPLICATION_PATH."/models/News.php";
include_once APPLICATION_PATH."/models/News_Category.php";
class  Admin_NewsController extends Zend_Controller_Action {
    public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;  
        $this->modelMapper = new Model_NewsMapper();
        $this->model = new Model_News;
        $this->CatmodelMapper = new Model_News_CategoryMapper();
        $this->Catmodel = new Model_News_Category;
    }
    public function indexAction()
    {
        $this->view->items=$this->modelMapper->publicpublished(2);
    }
    public function serviceAction(){
        $this->_helper->layout->disableLayout();        
        foreach($this->modelMapper->fetchAll() as $item)
        {
            $menber[] = array(
                'id'=> $item->getId(),
                'title'=> $item->getTitle(),
                'description'=> $item->getDescription(),       
                'news_cat_name'=> GlobalLib::getName("news_category", $item->getNews_Cat_Id(), "name"),
                'event'=> $item->getEvent(),
                'status'=> $item->getStatus()
            );
        }
        echo json_encode($menber);
        exit();
    }
    public function listAction() {
        
    }
    public function addAction()
    {        
        if($this->getRequest()->isPost())
        {
            $redirect = $this->aConfig['site']['url'].'admin/news/list';
            if(isset($_POST['title']))
                $this->model->setTitle($_POST['title']);
            if(isset($_POST['description']))
                $this->model->setDescription($_POST['description']);
            if(isset($_POST['content']))
                $this->model->setContent($_POST['content']);
            if(isset($_POST['news_cat_id']))
                $this->model->setNews_Cat_Id($_POST['news_cat_id']);
            
            if($_POST['event']==TRUE)
                $event = 1;
            else $event = 0;
            $this->model->setEvent($event);
            
            if($_POST['status']==TRUE)
                $status = 1;
            else $status = 0;
            $this->model->setStatus($status);
            
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            
            $this->modelMapper->save($this->model);
            $this->redirect($redirect);
        }
    }
    
    public function editAction()
    {
        $redirect = $this->aConfig['site']['url'].'admin/news/list';
        $id= $this->getParam("id","0");
        if($id == "0") $this->redirect ($redirect);
        $this->modelMapper->find($id,$this->model);
        $this->view->item = $this->model;        
        if($this->getRequest()->isPost())
        {            
            if(isset($_POST['title']))
                $this->model->setTitle($_POST['title']);
            if(isset($_POST['description']))
                $this->model->setDescription($_POST['description']);
            if(isset($_POST['content']))
                $this->model->setContent($_POST['content']);
            if(isset($_POST['news_cat_id']))
                $this->model->setNews_Cat_Id($_POST['news_cat_id']);
            
            if($_POST['event']==TRUE)
                $event = 1;
            else $event = 0;
            $this->model->setEvent($event);
            
            if($_POST['status']==TRUE)
                $status = 1;
            else $status = 0;
            $this->model->setStatus($status);
            
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            
            $this->modelMapper->save($this->model);
            $this->redirect($redirect);
        }                
    }
    public function deleteAction()
    {
        $redirect = $this->aConfig['site']['url'].'admin/news/list'; 
        $id=$this->getParam("id","0");               
        if($id != "0")
            $this->modelMapper->delete($id);
        $this->redirect($redirect);
    }
}
